package net.zoneland.knowledge.controller;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;

import net.zoneland.knowledge.model.LoginUserPojo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.base.Preconditions;

import net.zoneland.knowledge.controller.bean.UomUserOrgQueryVo;
import net.zoneland.knowledge.enums.PartyOrgEnum;
import net.zoneland.knowledge.model.PartyOrgVoForSelect;
import net.zoneland.knowledge.model.ResponseResult;
import net.zoneland.knowledge.service.PartyService;

/**
 * 党组织/党员选择框查询控制器.
 * @author bcc
 */
@RestController
@RequestMapping("/partyOrgOrUserSelect")
public class PartyOrgOrUserSelectController extends AbstractBaseController {
    /**
     * 日志.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(PartyOrgOrUserSelectController.class);


    @Autowired
    private PartyService partyService;

    /**
     * 查询人员相关信息.
     * @param request 请求.
     * @param vo 查询参数.
     * @return 人员信息
     */
    @PostMapping("/queryUsers")
    public ResponseResult queryUsers(final HttpServletRequest request, final @RequestBody UomUserOrgQueryVo vo) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.info("queryUsers param: {}", vo);
        }
        Preconditions.checkNotNull(vo, "查询参数对象不能为空！");
        getAndCheckCurrentUser(request);
        final ResponseResult res = new ResponseResult();
        res.setData(partyService.searchUserByConditionForTree(vo));
        return res;
    }

    /**
     * 查询党组织相关信息.
     * @param request 请求.
     * @param vo 查询参数.
     * @return 人员信息
     */
    @PostMapping("/queryDepts")
    public ResponseResult queryDepts(final HttpServletRequest request, final @RequestBody UomUserOrgQueryVo vo) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.info("queryDepts param: {}", vo);
        }
        Preconditions.checkNotNull(vo, "查询参数对象不能为空！");
        getAndCheckCurrentUser(request);
        final ResponseResult res = new ResponseResult();
        if (vo.getLevel() == 1) {
            final List<PartyOrgVoForSelect> lists = Arrays.stream(PartyOrgEnum.values())
                .sorted(Comparator.comparingInt(PartyOrgEnum::getOrder))
                .map(item -> {
                    PartyOrgVoForSelect partyOrgVoForSelect = new PartyOrgVoForSelect();
                    partyOrgVoForSelect.setOrgUUID(item.getOrgUuid());
                    partyOrgVoForSelect.setOrgShortName(item.getOrgName());
                    partyOrgVoForSelect.setLeaf(false);
                    return partyOrgVoForSelect;
                })
                .collect(Collectors.toList());
            res.setData(lists);
            return res;
        }
        res.setData(partyService.searchOrgByConditionForTree(vo));
        return res;
    }

    /**
     * 查询当前用户的群组名称.
     * @param request 请求.
     * @return 人员信息
     */
    @PostMapping("/queryGroupName")
    public ResponseResult queryGroupName(final HttpServletRequest request) {
        final LoginUserPojo user = getAndCheckCurrentUser(request);
        final ResponseResult res = new ResponseResult();
        res.setData(partyService.queryGroupName(user.getUserID()));
        return res;
    }

    /**
     * 根据群组ID查询成员信息.
     * @param request 请求.
     * @param param 查询参数
     * @return 群组名称列表
     */
    @PostMapping("/queryGroupMember")
    public ResponseResult queryGroupMember(final HttpServletRequest request, final @RequestBody UomUserOrgQueryVo param) {
        getAndCheckCurrentUser(request);
        Preconditions.checkNotNull(param.getGroupId(), "查询参数对象不能为空！");
        final ResponseResult res = new ResponseResult();
        res.setData(partyService.queryGroupMemberByGroupId(param.getGroupId()));
        return res;
    }

}
